package com.qfjy.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<String, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);

        //改变RedisTemplate默认JDK序列化
        StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();
        //Redis String key 字符串序列化
        template.setKeySerializer(stringRedisSerializer);
        //Redis String value字符串序列化
        template.setValueSerializer(stringRedisSerializer);

        //Redis Hash key 字符串序列化
        template.setHashKeySerializer(stringRedisSerializer);
        //Redis Hash value (一般适合存储一个对象） -》JSON格式。
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer=new GenericJackson2JsonRedisSerializer();
        template.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        return template;
    }

}
